package smartcrud.spring.controllers.rbac.editor;

import java.beans.PropertyEditorSupport;

import smartcrud.spring.models.rbac.Menu;
import smartcrud.spring.models.rbac.MenuService;
/**
 * http://stackoverflow.com/questions/8700339/spring-mvc-usage-of-formcheckbox-to-bind-data
 * @author cyper.yin
 *
 */
public class MenuEditor extends PropertyEditorSupport {
	private MenuService menuService;

	public MenuEditor(MenuService menuService) {
		this.menuService = menuService;
	}

	@Override
	public void setAsText(String text) throws IllegalArgumentException {
		if (text != null) {
			Menu menu = menuService.findMenuById(Long.valueOf(text));
			setValue(menu);
		} else {
			setValue(null);
		}
	}
}
